﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		ReferrerService.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using Dexter.Async;
using Dexter.Async.Services;
using Dexter.Domain.Model;
using Dexter.Domain.Results;
using Dexter.Repository;

namespace Dexter.Services.Implementation {
	/// <summary>
	/// 	The implementation of <see cref = "IReferrerService" />.
	/// </summary>
	public class ReferrerService : IReferrerService {
		protected IReferrerRepository ReferrerRepository { get; set; }
		protected IAsyncService AsyncService;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:System.Object"/> class.
		/// </summary>
		public ReferrerService ( IAsyncService asyncService , IReferrerRepository referrerRepository ) {
			this.AsyncService = asyncService;
			ReferrerRepository = referrerRepository;
		}

		/// <summary>
		/// Retireve a paged list of all referrers.
		/// </summary>
		/// <param name="pageIndex">Index of the page (0 based).</param>
		/// <param name="pageSize">The number of items to retrieve.</param>
		/// <returns>
		/// An concrete implementation of <see cref="IPagedResult"/>. The result will never be null.
		/// </returns>
		public IPagedResult <Referrer> GetCompleteList ( int pageIndex , int pageSize ) {
			return GetCompleteList ( pageIndex , pageSize , false );
		}

		/// <summary>
		/// Retireve a paged list of all referrers.
		/// </summary>
		/// <param name="pageIndex">Index of the page (0 based).</param>
		/// <param name="pageSize">The number of items to retrieve.</param>
		/// <param name="enableTracking">if set to <c>true</c> the tracking system of the return object will be enable.</param>
		/// <returns>
		/// An concrete implementation of <see cref="IPagedResult{T}"/>. The result will never be null.
		/// </returns>
		public IPagedResult <Referrer> GetCompleteList ( int pageIndex , int pageSize , bool enableTracking ) {
			return ReferrerRepository.GetReferrers ( pageIndex , pageSize , enableTracking );
		}

		/// <summary>
		/// 	Save the passed <see cref = "Referrer" /> to the repository
		/// </summary>
		/// <param name = "referrer">The referrer.</param>
		/// <returns>
		/// 	An instance of <see cref = "VoidAsyncResult" />. The resul will never be null.
		/// </returns>
		public virtual IAsyncResult AddReferrerAsync ( Referrer referrer ) {
			return AsyncService.AsyncCall ( SaveReferrertoRepository, referrer );
		}

		void SaveReferrertoRepository ( object state ) {
			var referrer = ( Referrer ) state;

			ReferrerRepository.Save ( referrer );
		}
	}
}
